# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Brisingr Aerowing <ztgreve@live.com>

_realname=sqlite3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-sqlite-docs"))
_sqlite_year=2025
_amalgamationver=3490200
_docver=${_amalgamationver}
pkgver=3.49.2
pkgrel=1
pkgdesc="A C library that implements an SQL database engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.sqlite.org/"
msys2_repository_url="https://www.sqlite.org/cgi/src/dir?ci=trunk"
msys2_references=(
  'archlinux: sqlite'
  "cpe: cpe:/a:sqlite:sqlite"
)
license=(PublicDomain)
depends=("${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-zlib")
# dlfcn: it checks for dlopen in autosetup, but then doesn't use it, so just a build dep
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-tcl"
             "${MINGW_PACKAGE_PREFIX}-dlfcn")
source=(https://www.sqlite.org/${_sqlite_year}/sqlite-src-${_amalgamationver}.zip
        https://www.sqlite.org/${_sqlite_year}/sqlite-doc-${_docver}.zip
        0001-sqlite-pcachetrace-include-sqlite3.patch
        0002-fix-building-sqldiff.patch
        0003-do-not-install-tclsqlite3-with-make.patch
        0004-dont-misuse-tcl-extern.patch
        Makefile.ext.in
        README.md.in
        LICENSE)
sha256sums=('c3101978244669a43bc09f44fa21e47a4e25cdf440f1829e9eff176b9a477862'
            '8568b11c76856de357a71fdcc02cfdfb210b4ec62f98bfb7af55211ed6540b83'
            '6518119034ceb2820d058afcb099d11f636271f55a41ffae22855af66a369166'
            'cf5c47c30e97f5493d2fad730a9bfcd33d20a0a052571e8e35b88f388d368724'
            'a500428a3434075932de84247b1405f3d73e7ed0a8b664eb20a736bd02d6ed29'
            '8dd2377afaad17fa85049be90bfc2bd757350343b3f64ff9bfbbcf3736593a34'
            '84b64569978dde5b63799099b0fbb3336943ef877170beb84483020c67e25b3c'
            '5ca42f1f92abfb61bacc9ff60f5836cc56e2ce2af52264f918cb06c3d566d562'
            '0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f')

# From Debian sqlite3-tools
_sqlite_tools="showdb.exe showjournal.exe showstat4.exe showwal.exe dbhash.exe sqldiff.exe sqlite3_analyzer.exe"

prepare() {
  cd "sqlite-src-${_amalgamationver}"
  patch -p1 -i "${srcdir}/0001-sqlite-pcachetrace-include-sqlite3.patch"
  patch -p1 -i "${srcdir}/0002-fix-building-sqldiff.patch"
  patch -p1 -i "${srcdir}/0004-dont-misuse-tcl-extern.patch"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a SQLITE_OPTIONS=(
            "-DSQLITE_ENABLE_COLUMN_METADATA=1"
            "-DSQLITE_USE_MALLOC_H=1"
            "-DSQLITE_USE_MSIZE=1"
            "-DSQLITE_DISABLE_DIRSYNC=1"
            "-DSQLITE_ENABLE_DBSTAT_VTAB=1"
            "-DSQLITE_SOUNDEX=1"
            "-DSQLITE_ENABLE_MATH_FUNCTIONS=1"
  )

  CFLAGS+=" -fexceptions -fno-strict-aliasing ${SQLITE_OPTIONS[@]}"

  MSYS2_ARG_CONV_EXCL="--prefix=;TCLLIBDIR=" \
    ../sqlite-src-${_amalgamationver}/configure \
      --prefix=${MINGW_PREFIX} \
      --enable-threadsafe \
      --disable-editline \
      --enable-readline \
      --enable-load-extension \
      --enable-tcl \
      --all \
      --session \
      --out-implib \
      --with-readline-inc=-I${MINGW_PREFIX}/include \
      --with-tcl=${MINGW_PREFIX}/lib \
      --with-tclsh=${MINGW_PREFIX}/bin/tclsh \
      TCLLIBDIR="${MINGW_PREFIX}/lib/sqlite${pkgver}"

  make

  # Build additional tools
  make ${_sqlite_tools}

  # Building the extensions
  mkdir -p ext/misc
  cp ../Makefile.ext.in ext/misc/Makefile
  sed -e "s|@srcdir@|${srcdir}/sqlite-src-${_amalgamationver}/ext/misc|g" -i ext/misc/Makefile
  sed -e "s|@top_srcdir@|${srcdir}/sqlite-src-${_amalgamationver}|g" -i ext/misc/Makefile
  sed -e "s|@top_builddir@|${PWD}|g" -i ext/misc/Makefile
  sed -e "s|@prefix@|${MINGW_PREFIX}|g" -i ext/misc/Makefile
  sed -e "s|@datadir@|${MINGW_PREFIX}/share|g" -i ext/misc/Makefile
  sed -e "s|@CC@|cc|g" -i ext/misc/Makefile
  make -C ext/misc
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  # This test run lasts very loooong ... despite the target name
  make quicktest || warning "Tests failed"
}

package_sqlite3() {
  optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: for sqlite3_analyzer")
  provides=(${MINGW_PACKAGE_PREFIX}-sqlite=${pkgver}
            ${MINGW_PACKAGE_PREFIX}-sqlite-analyzer=${pkgver})
  conflicts=(${MINGW_PACKAGE_PREFIX}-sqlite-analyzer)
  replaces=(${MINGW_PACKAGE_PREFIX}-sqlite-analyzer)

  cd "build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  for tools in ${_sqlite_tools}; do
    install -Dm755 $tools "${pkgdir}${MINGW_PREFIX}/bin/$tools"
  done

  # Install extensions
  make -C ext/misc DESTDIR="${pkgdir}" install
  cat "${srcdir}/README.md.in" | sed        \
    -e "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" \
    -e "s|@MSYSTEM@|${MSYSTEM}|g;"          \
    -e "s|@VERSION@|${pkgver}|g;"           \
    > "${pkgdir}${MINGW_PREFIX}/share/sqlite/extensions/README.md"

  install -Dm644 "${srcdir}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_sqlite-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  # Install docs
  cd "${srcdir}"/sqlite-doc-${_docver}
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}
  cp -R *  "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
